package com.fr.web.core.service;

import com.fr.base.ColumnRow;
import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.FRCoreContext;
import com.fr.base.core.RSADecode;
import com.fr.base.core.html.Html;
import com.fr.base.core.html.Tag;
import com.fr.base.core.html.TextHtml;
import com.fr.base.core.json.JSONObject;
import com.fr.report.FineBook;
import com.fr.report.ReportPage;
import com.fr.report.ReportPageAttr;
import com.fr.report.ReportWebAttr;
import com.fr.report.core.ReportUtils;
import com.fr.report.io.HTMLExportAttr;
import com.fr.report.io.ReportExportAttr;
import com.fr.util.Consts;
import com.fr.util.Utils;
import com.fr.web.Browser;
import com.fr.web.Repository;
import com.fr.web.core.HTMLConverter;
import com.fr.web.core.Service;
import com.fr.web.core.SessionIDInfor;
import com.fr.web.core.WebUtils;
import com.fr.web.core.chwriter.PageCellWriter;
import java.awt.Image;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PageService
  implements Service
{
  private static PageService PAGE_SERVICE = new PageService();
  private static boolean registed = false;

  public static PageService getInstance()
  {
    return PAGE_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return paramString.startsWith("page_");
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString1, String paramString2)
    throws Exception
  {
    if ("page_content".equals(paramString1))
      readPageContent(paramString2, paramHttpServletRequest, paramHttpServletResponse);
  }

  private static void readPageContent(String paramString, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    label40: Object localObject3;
    int i = 0;
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "pn");
    if (str != null)
    {
      localObject1 = Utils.string2Number(str);
      if (localObject1 != null)
        if ((i = ((Number)localObject1).intValue()) >= 0)
          break label40;
      i = 2147483647;
    }
    Object localObject1 = SessionDealWith.getSessionIDInfor(paramString);
    if (localObject1 == null)
      return;
    ReportPage localReportPage = ((SessionIDInfor)localObject1).getReportPage(i);
    if (localReportPage == null)
      return;
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    int j = localReportPage.getCurrentPageNumber();
    int k = localReportPage.getTotalPages();
    int l = ((SessionIDInfor)localObject1).getPageOwnerIndex(localReportPage);
    new Tag("script").sub(new TextHtml("FR._p=FR.SessionMgr.get(\"" + paramString + "\");" + "FR._p.currentPageIndex=" + j + ";" + "FR._p.reportTotalPage=" + k + ";" + "FR._p.ownerIndex=" + l + ";" + "delete FR._p;")).writeHtml(localPrintWriter);
    Tag localTag = new Tag("div").cls("pageContentDIV");
    Repository localRepository = new Repository(paramHttpServletRequest, (SessionIDInfor)localObject1);
    ColumnRow localColumnRow = (localReportPage.getReportPageAttr() == null) ? null : localReportPage.getReportPageAttr().getFreezeColumnRow();
    FRContext.getLogger().info(Inter.getLocText("INFO-Start_Converting_ReportPage_To_Html"));
    long l1 = System.currentTimeMillis();
    PageCellWriter localPageCellWriter = new PageCellWriter(localRepository, 0, false);
    if (localRepository.getBrowser().shouldConsiderHeavyTD())
    {
      localObject2 = ((SessionIDInfor)localObject1).getContextBook().getReportExportAttr();
      if ((localObject2 != null) && (((ReportExportAttr)localObject2).getHtmlExportAttr() != null))
      {
        localObject3 = ((ReportExportAttr)localObject2).getHtmlExportAttr();
        if (((HTMLExportAttr)localObject3).isTDHeavy())
          localPageCellWriter.setHeavy(true);
      }
    }
    Object localObject2 = localRepository.getSessionIDInfor().getContextBook().getReportWebAttr();
    if (ReportUtils.isWebPageModel((ReportWebAttr)localObject2))
      localTag.cls("contentDIV").css("width", localReportPage.getPageWidth() + "px").css("height", localReportPage.getPageHeight() + "px");
    if (ReportUtils.isWebPagePaintModel((ReportWebAttr)localObject2))
    {
      localObject3 = localReportPage.toImage(false);
      localTag.css("background-repeat", "no-repeat");
      localRepository.writeTagBackground(localTag, (Image)localObject3);
    }
    else
    {
      localObject3 = new HTMLConverter(localReportPage, localPageCellWriter, localColumnRow, localRepository, localTag, true);
      ((HTMLConverter)localObject3).convert();
    }
    if (localRepository.getBrowser().shouldWrapCenter())
    {
      localObject3 = localTag;
      localTag = new Tag("center").sub((Html)localObject3);
    }
    localTag.writeHtml(localPrintWriter);
    SessionDealWith.writeRegistionDiv(paramHttpServletRequest, localPrintWriter);
    localPrintWriter.flush();
    localPrintWriter.close();
    long l2 = System.currentTimeMillis();
    FRContext.getLogger().info(Inter.getLocText("INFO-End_Converting_ReportPage_To_Html") + "\t" + Inter.getLocText("INFO-Time_Taken") + (l2 - l1) + Inter.getLocText("Milliseconds"));
  }

  static
  {
    byte[] arrayOfByte = FRCoreContext.getBytes();
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      RSADecode.decode(arrayOfByte, localByteArrayOutputStream);
    }
    catch (Exception localException1)
    {
    }
    try
    {
      JSONObject localJSONObject = new JSONObject(new String(localByteArrayOutputStream.toByteArray(), "UTF-8"));
      registed = (localJSONObject.getLong("deadline") > Calendar.getInstance().getTimeInMillis()) && (localJSONObject.has("version")) && (ComparatorUtils.equals(Consts.VERSION, localJSONObject.getString("version"))) && (((localJSONObject.getLong("deadline") - Calendar.getInstance().getTimeInMillis() < 3153600000L) || ((localJSONObject.has("macaddress")) && (BaseCoreUtils.getMacAddresses().contains(localJSONObject.getString("macaddress"))))));
    }
    catch (Exception localException2)
    {
    }
  }
}